var checkedNodes = [];
layui.use(['element', 'table', 'form', 'layer', 'upload'], function () {
    var element = layui.element;
    var $ = layui.$;
    var form = layui.form;
    var table = layui.table;
    var layer = layui.layer;
    var upload = layui.upload;
    //获取创建人id
    var createId = localStorage.getItem('createId');
    $('input[name=createId]').val(createId);
    //上传图片
    var uploadInst = upload.render({
        elem: "#test",
        url: "/upload/uploadFile",
        multiple: true,
        number:6,
        size:12288,
        field: "files",
        accept: "images",
        done: function (res, index, upload) {
            var mainImage = res.data.path;
            $('#image').prepend('<img src="' + res.data.path
                + '" height="100px" width="100px" border="1px solid #ccc" class="layui-upload-img uploadImgPreView">');
            $('input[name=mainImage]').val(mainImage);
        },
        progress: function (n, elem, e) {
            element.progress('demo', n + '%');
            if (n == 100) {
                layer.msg('上传完毕', {icon: 1});
            }
        }
    });
    /*商品分类*/
    findType = function findType() {
        layer.open({
            type: 1,
            title: '选择分类',
            area: ['500px', '400px'],
            maxmin: true,
            shade: 0,
            offset: 'auto',
            content: '<ul id="ztree" class="ztree"></ul>',
            btn: ['确定', '取消'],
            yes: function (index, layero) {
                $('input[name=title]').val(title);
                $('input[name=categroyId]').val(categroyId);
                $('#sort').show();
                layer.close(index);
            }
        });
        var settingss = {
            data: {
                simpleData: {
                    enable: true,  //true 、 false 分别表示 使用 、 不使用 简单数据模式
                    idKey: "categroyId",   //节点数据中保存唯一标识的属性名称
                    pIdKey: "parentId",    //节点数据中保存其父节点唯一标识的属性名称
                    rootPId: -1  //用于修正根节点父节点数据，即 pIdKey 指定的属性值
                },
                key: {
                    name: "title"  //zTree 节点数据保存节点名称的属性名称  默认值："name"
                }
            },
            check: {
                enable: true,
                chkStyle: 'checkbox',
                chkboxType:{
                    'Y':'ps',
                        'N':'s'
                },
                nocheckInherit: true   //当父节点设置 nocheck = true 时，设置子节点是否自动继承 nocheck = true
            },
            callback: {
                onCheck: onCheck,
                onClick: chooseNode
            }
        };
        var title = "";
        var categroyId = "";
        //点击复选框
        function onCheck(e, treeId, treeNode) {
            var treeObj = $.fn.zTree.getZTreeObj("ztree"),
                nodes = treeObj.getCheckedNodes(true),
                v = "";
            checkedNodes = [];
            for (var i = 0; i < nodes.length; i++) {
                v += nodes[i].title + ">";
                title = nodes[i].title;
                categroyId = nodes[i].categroyId;
                checkedNodes += nodes[i].categroyId+'';
            }
            var leng = v.substring(v.indexOf('>')+1);
            v = leng.substring(0,leng.length-1)
            $('#type').text(v);
        }
        //点击节点
        function chooseNode(event, treeId, treeNode) {
            $('#type').text(treeNode.title);
            $('input[name=title]').val(treeNode.title);
            $('input[name=categroyId]').val(treeNode.categroyId);
            $('#sort').show();
            layer.close(layer.index);
        }
        //加载树的数据
        $.ajax({
            url: "/goodsTree/getTree",
            type: 'post',
            dataType: 'json',
            success: function (data) {
                zTreeObj = $.fn.zTree.init($("#ztree"), settingss, data.data); //初始化树
                zTreeObj.expandAll(true);   //true 节点全部展开、false节点收缩
                // 回显选中的数据
                var treeObj = $.fn.zTree.getZTreeObj("ztree");
                for(var i = 0; i < checkedNodes.length; i++) {
                    var rnode = treeObj.getNodeByParam("categroyId", checkedNodes[i]);
                    treeObj.checkNode(rnode,true);
                }
            }
        });
    }

    //提交
    $('#submit').on('click', function () {
        var title = $('input[name=title]').val();
        var productName = $('input[name=productName]').val();
        var mainImage = $('input[name=mainImage]').val();
        var productDescription = $('textarea[name=productDescription]').val();
        var productLabel = $('textarea[name=productLabel]').val();
        var productCode = $('input[name=productCode]').val();
        var bCode = isNaN(productCode);
        var originalpriceProduct = $('input[name=originalpriceProduct]').val();
        var bprice = isNaN(originalpriceProduct);
        var searchKeywords = $('input[name=searchKeywords]').val();
        if (title == '' | productName == '' | mainImage == '' | productDescription == '' | productLabel == '' |
            productCode==''|originalpriceProduct==''|searchKeywords==''|bCode==true|bprice==true){
            layer.msg("信息填写完整");
            return;
        }
        var paramProduct = $('#form').serialize();
        $.ajax({
            url: '/goodsTable/addProduct',
            type: 'post',
            dataType: 'json',
            async: false,
            data: paramProduct,
            success: function (data) {
                var createId = $('input[name=createId]').val();
                var paramRecord = {
                    'courseId': data.data.productId,
                    'launchId': createId,
                    'result': '2',
                    'type': 3
                }
                $.ajax({
                    url: '/Record/addJsCourseRecord',
                    type: 'post',
                    dataType: 'json',
                    async: false,
                    data: paramRecord,
                    success: function (data) {
                        parent.$('#quickHref').val('商品列表');
                        parent.getQuick();
                        return;
                    }
                })
            }
        });
    });
});

//返回
function goBack() {
    parent.$('#quickHref').val('商品列表');
    parent.getQuick();
    return;
}
